package com.wf.ew.daily.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.DateUtil;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.daily.biz.ScheduleBiz;
import com.wf.ew.daily.model.Schedule;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-01-05 11:22:55
 * @version ：1.0
 */
@Repository
public class ScheduleBizImpl extends BaseDao implements ScheduleBiz {

	@Override
	public String list(PageResult<T> page, Schedule schedule, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Schedule> rm = new BeanPropertyRowMapper<Schedule>(Schedule.class);
		StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.scheduleDate");
        sql.append(",s.times,s.type,s.content,s.state");
		sql.append(",s.createTime,s.createUser,s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_schedule s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(schedule)){
//			if(Utils.isNotEmpty(schedule.getName())){
//				sql.append(" and instr(s.name,:name)>0 ");
//				map.put("name", schedule.getName());
//			}
			if(Utils.isNotEmpty(schedule.getSort())&&Utils.isNotEmpty(schedule.getOrder())){
				sql.append(" order by "+schedule.getSort()+" "+schedule.getOrder());
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Schedule> list(Schedule schedule, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Schedule> rm = new BeanPropertyRowMapper<Schedule>(Schedule.class);
        StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.scheduleDate");
        sql.append(",s.times,s.type,s.content,s.state");
		sql.append(",s.createTime,s.createUser,s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_schedule s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and s.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(schedule)){
//			if(Utils.isNotEmpty(schedule.getName())){
//				sql.append(" and instr(s.name,:name)>0 ");
//				map.put("name", schedule.getName());
//			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Schedule getById(Schedule schedule) {
		RowMapper<Schedule> rm = new BeanPropertyRowMapper<Schedule>(Schedule.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select s.id,s.companyId,s.scheduleDate");
        sql.append(",s.times,s.type,s.content,s.state");
		sql.append(",s.createTime,s.createUser,s.updateTime,s.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_schedule s ");
        sql.append(" inner join sys_user suser on suser.id=s.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=s.companyId ");
        sql.append(" where s.del=0 and cmy.del=0 and s.id=:id ");
		map.put("id", schedule.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean add(Schedule schedule, User loginUser) {
		int count = 1;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_schedule (");
        sql.append("companyId,scheduleDate,times,type");
		sql.append(",content,state,createUser) values ( ");
        sql.append(":companyId,:scheduleDate,:times,:type");
		sql.append(",:content,:state,:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("scheduleDate", schedule.getScheduleDate());
        map.put("times", schedule.getTimes()==null?null:schedule.getTimes()+"");
        map.put("type", schedule.getType());
        map.put("content", schedule.getContent());
        map.put("state", schedule.getState());
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Schedule schedule, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_schedule set ");
        sql.append("scheduleDate=:scheduleDate,times=:times,type=:type ");
		sql.append(",content=:content,state=:state,updateTime=now() ");
		sql.append(",updateUser=:updateUser where id=:id ");
        map.put("scheduleDate", schedule.getScheduleDate());
        map.put("times", schedule.getTimes()==null?null:schedule.getTimes()+"");
        map.put("type", schedule.getType());
        map.put("content", schedule.getContent());
        map.put("state", schedule.getState());
		map.put("updateUser", loginUser.getId());
		map.put("id", schedule.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(Schedule schedule, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(schedule.getIds().length()>0){
			for(String ID : schedule.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_schedule set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
